<?php
namespace app\admin\controller;
use app\admin\business\tradeBus;
use app\admin\validate\TradeChild;
use think\facade\View;

class Trade extends AdminBase
{
    public function index(){
        return View::fetch();
    }
    public function add(){
        return View::fetch();
    }
    public function addTrade(){
        $name = $this->request->param("name");
        $data = [
            'name'=>$name
        ];
        $validate = new \app\admin\validate\Trade();
        if(!$validate->scene('trade_add')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $tradeBus = new \app\admin\business\tradeBus;
        $res = $tradeBus->addTradeBus($data);
        return show(config('status.success'),'添加成功',$res);       
    }
    public function getTradeList(){
        $tradeBus = new tradeBus();
        $result = $tradeBus->getTradeList();
        if(empty($result)){
            throw new \think\Exception('未查询到数据');
        }
        return show(config('status.success'),'添加成功',$result);;
    }
    public function edit(){
        $id = $this->request->param('id');
        View::assign('id',$id);
        return View::fetch();
    }
    public function getTradeById(){
        $id = $this->request->param('id');
        $where = [
            "id" => $id
        ];
        $tradeBus = new tradeBus();
        $result = $tradeBus->getTradeById($where);
        if(empty($result)){
            throw new \think\Exception('未查询到数据');
        }
        return show(config('status.success'),'添加成功',$result);
    }
    public function updateTrade(){
        $id = $this->request->param('id');
        $name = $this->request->param('name');
        $id = [
            "id" => $id
        ];
        $data = [
            'name' => $name
        ];
        $tradeBus = new tradeBus();
        try {
            $result = $tradeBus->updateTrade($id,$data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }

        return show(config('status.success'),'更新成功',$result);
    }
//    --------------------------------------------------------------------------------------------------

    public function childList(){
        return View::fetch();
    }
    public function getTradeChildListPage(){
        $current_page = $this->request->param('current_page');
        $num = $this->request->param('num');
        $tradeBus = new tradeBus();
        try {
            $result = $tradeBus->getNewsList($current_page,$num);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'获取成功',$result);
    }
    public function childAdd(){
        return View::fetch();
    }
    public function addChildTrade(){
        $name = $this->request->param("name");
        $trade_id = $this->request->param("trade_id");
        $data = [
            'name'=>$name,
            "trade_id" => $trade_id
        ];
        $validate = new \app\admin\validate\TradeChild();
        if(!$validate->scene('trade_child_add')->check($data)){
            return show(config('status.error'),$validate->getError());
        }
        $tradeBus = new \app\admin\business\tradeBus;
        $res = $tradeBus->addTradeChild($data);
        return show(config('status.success'),'添加成功',$res);
    }
    public function childEdit(){
        $id = $this->request->param('id');
        View::assign('id',$id);
        return View::fetch();
    }
    public function getTradeChildById(){
        $id = $this->request->param('id');
        $where = [
            "id" => $id
        ];
        $tradeBus = new tradeBus();
        $result = $tradeBus->getTradeChildByIdBus($where);
        if(empty($result)){
            throw new \think\Exception('未查询到数据');
        }
        return show(config('status.success'),'添加成功',$result);
    }

    public function updateTradeChild(){
        $id = $this->request->param('id');
        $name = $this->request->param('name');
        $trade_id = $this->request->param('trade_id');
        $id = [
            "id" => $id
        ];
        $data = [
            'name' => $name,
            'trade_id'=>$trade_id
        ];
        try {
            validate(TradeChild::class)->scene('trade_child_add')->check($data);
        }catch (ValidateException $e){
            return show(config('status.error'),$e->getError());
        }
        $tradeBus = new tradeBus();
        try {
            $result = $tradeBus->updateTradeChild($id,$data);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }

        return show(config('status.success'),'更新成功',$result);
    }
    public function removeTradeChild(){
        $id = $this->request->param('id');
        $tradeBus = new tradeBus();
        try {
            $result = $tradeBus->removeTradeChild($id);
        }catch (\Exception $exception){
            return show(config('status.error'),$exception->getMessage());
        }
        return show(config('status.success'),'删除成功',$result);
    }

}
